#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>

#include "mv.h"

void cmd_mv(char *comm)
{
	char *argv[4];
	char *s;
	int nr=0, ch, i=0;
	argv[i] = strtok (comm, " ");
	nr++; i++;
	while ((argv[i++] = strtok (NULL, " ")) != NULL)
		nr++;
	if (nr < 3)
	{
		fprintf (stderr, "-invalid command!\n");
		fprintf (stderr, "-usage: mv file1 file2\n");
	}
	else {
		FILE *f = fopen (argv[1], "r");
		FILE *g = fopen (argv[2], "w");
		while ((ch = fgetc (f)) != EOF)
			fputc (ch, g);
		fclose (f);
		fclose (g);
		unlink (argv[1]);
	}
}

int main()
{
        char *comm = readline ("usage: mv file\ncommand: ");
	cmd_mv(comm);
	return 0;
}
